<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .parent{
        border:1px solid blue;
        height: 200px;
        margin: 10px;
    }
    .child{
        width: 100px;
        height: 100px;
        border:1px solid #333;
        float: left;
        margin: 10px;
    }
</style>
<body>
    <div class="parent"></div>
    <div class="parent"></div>
    <div class="child" id="one" draggable="true">1</div>
    <div class="child" id="two" draggable="true">2</div>
    <div class="child" id="three" draggable="true">3</div>
    <div class="child" id="four" draggable="true">4</div>
    <div class="child" id="five" draggable="true">5</div>
    <script>
console.log(Object)

        window.onload = function(){
            var body = document.body;
            // parents是所有的目标元素 两个parent一个body
            var parents = document.getElementsByClassName('parent');
            parents = [...parents];
            parents.push(body);

            parents.forEach(function(item){
                // 将目标元素设置为可被放置
                item.ondragover = function(){
                    event.preventDefault()
                }
                item.ondrop = function(){
                    var id = event.dataTransfer.getData('id')
                    var dom = document.getElementById(id)
                    event.target.appendChild(dom)
                    
                }
            }) 

            // childs是所有拖放元素，五个child
            var childs = document.getElementsByClassName('child');
            childs = [...childs];
            childs.forEach(function(item){
                item.ondragstart = function(){
                    event.dataTransfer.setData('id',this.id)
                }
            })



        }
    </script>
    
</body>
</html>